package com.chemical.search.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.chemical.search.model.UserInfo;


public class CheckLoginInterceptor extends HandlerInterceptorAdapter{
		
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		super.afterCompletion(request, response, handler, ex);
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		//过滤登录
		String requestUrl = request.getRequestURI();
		if(requestUrl.indexOf("login.do")>-1||requestUrl.indexOf("upload.do")>-1){
			return true;
		}		
		// 获得登陆时的用户
		UserInfo user = (UserInfo) request.getSession().getAttribute("userInfo");
		// 如果用户为null，则表明用户未登录
		if(null==user){
			response.sendRedirect("/chemSearch/manage/login.jsp");
			return false;
		}
		return true;
	}
	

}
